package com.sicnu.demo07

import android.content.Intent
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
    
    companion object {
        const val EXTRA_NAME = "extra_name"
        const val EXTRA_GENDER = "extra_gender"
        const val EXTRA_STUDENT_ID = "extra_student_id"
        const val EXTRA_PHONE = "extra_phone"
        const val EXTRA_EMAIL = "extra_email"
        const val EXTRA_HOBBY = "extra_hobby"
        const val EXTRA_HIGH_SCORE = "extra_high_score"
        const val REQUEST_EDIT_INFO = 1
        const val REQUEST_PLAY_GAME = 2
    }
    
    private lateinit var tvName: TextView
    private lateinit var tvGender: TextView
    private lateinit var tvStudentId: TextView
    private lateinit var tvPhone: TextView
    private lateinit var tvEmail: TextView
    private lateinit var tvHobby: TextView
    private lateinit var tvHighScore: TextView
    private lateinit var btnEditInfo: Button
    private lateinit var btnPlayGame: Button
    
    private var name = ""
    private var gender = ""
    private var studentId = ""
    private var phone = ""
    private var email = ""
    private var hobby = ""
    private var highScore = 0
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        
        initViews()
        initData()
        setListeners()
    }
    
    private fun initViews() {
        tvName = findViewById(R.id.tvName)
        tvGender = findViewById(R.id.tvGender)
        tvStudentId = findViewById(R.id.tvStudentId)
        tvPhone = findViewById(R.id.tvPhone)
        tvEmail = findViewById(R.id.tvEmail)
        tvHobby = findViewById(R.id.tvHobby)
        tvHighScore = findViewById(R.id.tvHighScore)
        btnEditInfo = findViewById(R.id.btnEditInfo)
        btnPlayGame = findViewById(R.id.btnPlayGame)
    }
    
    private fun initData() {
        name = getString(R.string.default_name)
        gender = getString(R.string.default_gender)
        studentId = getString(R.string.default_student_id)
        phone = getString(R.string.default_phone)
        email = getString(R.string.default_email)
        hobby = getString(R.string.default_hobby)
        highScore = 0
        
        updateUI()
    }
    
    private fun updateUI() {
        tvName.text = name
        tvGender.text = gender
        tvStudentId.text = studentId
        tvPhone.text = phone
        tvEmail.text = email
        tvHobby.text = hobby
        tvHighScore.text = highScore.toString()
    }
    
    private fun setListeners() {
        btnEditInfo.setOnClickListener {
            val intent = Intent(this, EditInfoActivity::class.java).apply {
                putExtra(EXTRA_NAME, name)
                putExtra(EXTRA_GENDER, gender)
                putExtra(EXTRA_STUDENT_ID, studentId)
                putExtra(EXTRA_PHONE, phone)
                putExtra(EXTRA_EMAIL, email)
                putExtra(EXTRA_HOBBY, hobby)
            }
            startActivityForResult(intent, REQUEST_EDIT_INFO)
        }
        
        btnPlayGame.setOnClickListener {
            val intent = Intent(this, CardGameActivity::class.java)
            startActivityForResult(intent, REQUEST_PLAY_GAME)
        }
    }
    
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        
        if (resultCode == RESULT_OK && data != null) {
            when (requestCode) {
                REQUEST_EDIT_INFO -> {
                    name = data.getStringExtra(EXTRA_NAME) ?: name
                    gender = data.getStringExtra(EXTRA_GENDER) ?: gender
                    studentId = data.getStringExtra(EXTRA_STUDENT_ID) ?: studentId
                    phone = data.getStringExtra(EXTRA_PHONE) ?: phone
                    email = data.getStringExtra(EXTRA_EMAIL) ?: email
                    hobby = data.getStringExtra(EXTRA_HOBBY) ?: hobby
                    updateUI()
                }
                REQUEST_PLAY_GAME -> {
                    val score = data.getIntExtra(EXTRA_HIGH_SCORE, 0)
                    if (score > highScore) {
                        highScore = score
                        tvHighScore.text = highScore.toString()
                    }
                }
            }
        }
    }
    
    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.menu_main, menu)
        return true
    }
    
    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        return when (item.itemId) {
            R.id.action_about -> {
                showAboutDialog()
                true
            }
            else -> super.onOptionsItemSelected(item)
        }
    }
    
    private fun showAboutDialog() {
        AlertDialog.Builder(this)
            .setTitle(R.string.about_dialog_title)
            .setMessage(R.string.about_dialog_message)
            .setPositiveButton(R.string.dialog_ok, null)
            .show()
    }
}
